/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.qiujuer.genius.ui.widget;

import ohos.agp.components.AbsButton;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Color;
import ohos.app.Context;

import net.qiujuer.genius.ui.AttrValue;
import net.qiujuer.genius.ui.drawable.CircleCheckDrawable;

import java.util.ArrayList;
import java.util.List;

/**
 * This is CheckBox widget
 * The widget extend ohos.agp.components.Checkbox
 *
 * @since 2021-05-10
 */
public class CheckBox extends Checkbox implements Component.DrawTask, AbsButton.CheckedStateChangedListener {
    private static final String DEFAULT_COLOR = "#9e9e9e";
    private static final int TEXT_SIZE = 50;
//    private Context context;
//    private AttrSet attrSet;
    private final List<CheckedStateChangedListener> checkedStateChangedListeners = new ArrayList<>();
    private CircleCheckDrawable circleCheckDrawable;
    private final int DEFAULT_BORDER_SIZE = AttrHelper.vp2px(1.5f, getContext());
    private final int DEFAULT_INTERVAL_SIZE = AttrHelper.vp2px(4, getContext());
    private final int DEFAULT_MARK_SIZE = AttrHelper.vp2px(6, getContext());

    /**
     * CheckBox
     *
     * @param context
     */
//    public CheckBox(Context context) {
//        this(context, null);
//    }

    /**
     * CheckBox
     *
     * @param context
     * @param attrSet
     */
    public CheckBox(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    /**
     * CheckBox
     *
     * @param context
     * @param attrSet
     * @param styleName
     */
    public CheckBox(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(attrSet);
    }

//    private void init(Context context, AttrSet attrSet, String styleName) {
//        this.context = context;
//        this.attrSet = attrSet;
//        initAttr(attrSet);
//    }

    private void init(AttrSet attrSet) {
        addDrawTask(this);
        setCheckedStateChangedListener(this);
        circleCheckDrawable = new CircleCheckDrawable(this);
        initAttr(attrSet);
        setButtonElement(circleCheckDrawable);
        addCheckedStateChangedListener(circleCheckDrawable);

        setTextSize(TEXT_SIZE);
    }

    private void initAttr(AttrSet attrSet) {
        Color checked_enable_element = AttrValue.getColorValue(attrSet, "checked_enable_element", DEFAULT_COLOR);
        Color unchecked_enable_element = AttrValue.getColorValue(attrSet, "unchecked_enable_element", DEFAULT_COLOR);
        Color checked_disable_element = AttrValue.getColorValue(attrSet, "checked_disable_element", DEFAULT_COLOR);
        Color unchecked_disable_element = AttrValue.getColorValue(attrSet, "unchecked_disable_element", DEFAULT_COLOR);
        circleCheckDrawable.setEnableCheckColor(checked_enable_element);
        circleCheckDrawable.setEnableUncheckColor(unchecked_enable_element);
        circleCheckDrawable.setUnableCheckColor(checked_disable_element);
        circleCheckDrawable.setUnableUncheckColor(unchecked_disable_element);
        int gBorderSize = AttrValue.getDimension(attrSet, "gBorderSize", DEFAULT_BORDER_SIZE);
        int gIntervalSize = AttrValue.getDimension(attrSet, "gIntervalSize", DEFAULT_INTERVAL_SIZE);
//        int gMarkSize = AttrValue.getDimension(attrSet, "gMarkSize", DEFAULT_MARK_SIZE);
        //String gFont = AttrValue.getStringValue(attrSet, "gFont", "");
        // todo set typeface for checkbox

        circleCheckDrawable.setBorderSize(gBorderSize);
        circleCheckDrawable.setIntervalSize(gIntervalSize);
    }

    /**
     * 设置可用选中颜色
     *
     * @param color 颜色
     */
    public void setEnableCheckColor(Color color) {
        circleCheckDrawable.setEnableCheckColor(color);
    }

    /**
     * 设置可用非选中颜色
     *
     * @param color 颜色
     */
    public void setEnableUncheckColor(Color color) {
        circleCheckDrawable.setEnableUncheckColor(color);
    }

    /**
     * 设置不可用选中颜色
     *
     * @param color 颜色
     */
    public void setUnableCheckColor(Color color) {
        circleCheckDrawable.setUnableCheckColor(color);
    }

    /**
     * 设置不可用非选中颜色
     *
     * @param color 颜色
     */
    public void setUnableUncheckColor(Color color) {
        circleCheckDrawable.setUnableUncheckColor(color);
    }

    @Override
    public void onCheckedChanged(AbsButton absButton, boolean isChecked) {
        for (CheckedStateChangedListener listener : checkedStateChangedListeners) {
            listener.onCheckedChanged(absButton, isChecked);
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        circleCheckDrawable.setEnabled(enabled);
        invalidate();
    }

    /**
     * 添加选中状态监听器
     *
     * @param checkedStateChangedListener 选中状态监听器
     */
    public void addCheckedStateChangedListener(CheckedStateChangedListener checkedStateChangedListener) {
        if (!checkedStateChangedListeners.contains(checkedStateChangedListener)) {
            checkedStateChangedListeners.add(checkedStateChangedListener);
        }
    }

    /**
     * 移除选中状态监听器
     *
     * @param checkedStateChangedListener 选中状态监听器
     */
    public void removeCheckedStateChangedListener(CheckedStateChangedListener checkedStateChangedListener) {
        checkedStateChangedListeners.remove(checkedStateChangedListener);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        circleCheckDrawable.onDraw(component, canvas);
    }
}
